// A0 A1 A2 A3 A4 A5
// 14 15 16 17 18 19
// ------------------------- 
//| C8 C7 R2 C1 R4 C6 C4 R1|
//| 18 16 19 15  0 14  2  1|
//|                        |
//| 10 12 13  4  9  3  8  7|
//| R5 R7 C2 C3 R8 C5 R6 R3|
// -------------------------
#include <IRremote.h>
#include "game.h"
#include "matrix.h"
#include "pin.h"
#define SPEED 500
unsigned char row[8] = {
  1,19,7,0,10,8,12,9}; 
unsigned char col[8] = {
  15,13,4,2,3,14,16,18};

IRrecv irrecv(RECV_PIN);
decode_results results;

Game* game = new Game;

unsigned long time;
void setup()
{
  randomSeed(analogRead(0));
  pinMode(BEEP,OUTPUT);
  for(int i=0;i<8;i++)
  {
    pinMode(row[i],OUTPUT);
    digitalWrite(row[i],LOW);
    pinMode(col[i],OUTPUT);
    digitalWrite(col[i],HIGH);
  }
  irrecv.enableIRIn(); // Start the receiver
  game->update();
  time = millis();
}

direction dir = RIGHT;
void loop()
{
  direction tempdir=dir;
  if (irrecv.decode(&results)) 
  {
    switch(results.value)
    {
    case 0xFF629D:
      if(dir!=DOWN)
        dir=UP;
      break;
    case 0xFFA857:
      if(dir!=UP)
        dir=DOWN;
      break;
    case 0xFF22DD:
      if(dir!=RIGHT)
        dir=LEFT;
      break;
    case 0xFFC23D:
      if(dir!=LEFT)
        dir=RIGHT;
      break;
    default:
      break;
    }
    irrecv.resume(); // Receive the next value
  }
  game->update();
  if(millis()-time>SPEED)
  {
    time = millis();

    game->run(dir);
    if(game->gameover)
    {
      game->over();
      delay(1000);
      game->restart();
      dir = RIGHT;
      time = millis();
    }
  }
}











